﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using AddIn.EA.Core.Common;

namespace AddIn.EA.Architecture.ExternalResources
{
    public class ProcessStructure
    {
        private const string XmlFileName = "PackageStructure.xml";
        private const string XmlSchemaFile = "PackageStructure.xsd";

        public static XmlDocument GetProcessStructureXml()
        {
            XmlSchema schema = null;
            string schemasPath = Path.Combine(AddInRegistryResources.GetBaseDirectory(), "Schemas");
            using (StreamReader reader = new StreamReader(Path.Combine(schemasPath, XmlSchemaFile), UTF32Encoding.UTF8))
            {
                schema = XmlSchema.Read(reader, ProcessStructure.XmlValidation);
            }

            string resourcesPath = Path.Combine(AddInRegistryResources.GetBaseDirectory(), "Resources");

            XmlDocument xml = new XmlDocument();
            xml.Schemas.Add(schema);
            xml.Load(Path.Combine(resourcesPath, XmlFileName));
            xml.Validate(ProcessStructure.XmlValidation);

            return xml;
        }

        private static void XmlValidation(object sender, ValidationEventArgs e)
        {
            throw new InvalidDataException(string.Format("The Process Structure Xml is invalid:\n\n{0}", e.Message));
        }
    }
}
